import { Switch, Route, Redirect, withRouter } from "react-router";
import { userinfo } from "../utils/getUserinfo"

function RouterView(props) {
    const { routes } = props
    return <Switch>
        {
            routes.map((item, index) => {



                if (item.component) {
                    return <Route key={index} exact={item.exact} path={item.path} render={() => {

                        if (item.meta.mustLogin) {
                            // const userinfo = JSON.parse(localStorage.getItem('logindata')||'{}');
                            if (!userinfo().token) {
                                return <Route key={index} exact={item.exact} path={item.path}>
                                    <Redirect to='/login' />
                                </Route>
                            }
                        }
                        item.meta.title && (document.title = item.meta.title) //设置title
                        const Component = withRouter(item.component)
                        return <Component {...props} />
                    }} />
                } else {
                    return <Route key={index} exact={item.exact} path={item.path}>
                        <Redirect to={item.redirect} />
                    </Route>
                }

            })
        }
    </Switch>
}

export default RouterView